<?php
/**
  * CLOUD
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
class Main
{
	public static function run()
	{
		try 
		{
			self::init();
			//self::param();
			self::session();
			
			$con = ucfirst( PARAM_C . 'Controller' );
			$act = PARAM_A;
		
			if ( file_exists( CONTROLLER . $con . '.php' ) )
			{
				$conObject = new $con();
				if ( method_exists( $conObject , $act) )
				{
					$conObject->$act();
				}
				else
				{
					Common::errorOutput( 'method not exists!' );
				}
			}
			else
			{
				Common::errorOutput( 'controller file not exists!' );
			}
		} 
		catch ( BaseException $e) 
		{
			//exit( 'errorno:' . $e->getCode() . "\t" . 'errorstr:' . $e->getMessage() );
			exit( '<div style="font-size:14px;text-align:left; border:1px solid #9cc9e0; padding:3px 4px;color:#000000;font-family:Arial, Helvetica,sans-serif;"><b>Error url : </b>' . $_SERVER['REQUEST_URI'] . '<br /><b> Error Message : </b>"' . $e->getMessage() . '" <br /> <b>Error code : </b>"' . $e->getCode() . '" <br /></div>' );
		}
	}
	
	public static function init()
	{
		define( "IN_CLOUD" , true );
		define( "DEBUG" , true );
		define( "ROOT_DIR" , str_replace( '\\', '/', dirname( __FILE__ ) . '/' ) );
		define( "CONTROLLER" , ROOT_DIR . 'Controller/' );
		define( "MODEL" , ROOT_DIR . 'Model/' );
		define( "LIB" , MODEL . 'Lib/' );
		define( "DATA" , 'Data/' );
		define( "TPL" , ROOT_DIR . 'Tpl/' );
		define( "TPLCOMPILE_DIR" , ROOT_DIR . DATA . 'Tplcompile/');
		define( "TPLCACHE_DIR" , ROOT_DIR . DATA . 'Tplcache/' );
		define( "LANG" , ROOT_DIR . 'Lang/' );
		define( "ATTACHMENT" , 'Attachment/' );
		define( "STATICS" , ROOT_DIR . "Statics/" );
		define( "PARAM_C" , isset( $_GET['c'] ) ? $_GET['c'] : ( isset( $_POST['c'] ) ? $_POST['c'] : 'index' ) );
		define( "PARAM_A" , isset( $_GET['a'] ) ? $_GET['a'] : ( isset( $_POST['a'] ) ? $_POST['a'] : 'run' ) );
		
		define( "SYSTIME" , time() );
		define( "MICTOTIME" , microtime( true ) );
		
		DEBUG ? error_reporting( E_ALL ^ E_NOTICE ) : error_reporting( 0 );
		
		define( "SITEURL" , Common::getConfig( 'System' , 'site_url' ) );
		define( "SITENAME" , Common::getConfig( 'System' , 'site_name' ) );
		define( "CSSURL" , Common::getConfig( 'System' , 'css_url' ) );
		define( "JSURL" , Common::getConfig( 'System' , 'js_url') );
		define( "IMAGEURL" , Common::getConfig('System' , 'image_url') );
		define( "ATTACHMENTURL" , Common::getConfig('System' , 'attachment_url') );
		define( "STYLE" , ucfirst( Common::getConfig( 'System' , 'style' ) ) );
		define( "CHARSET" , 'UTF-8' );
		define( "PRE" , Common::getConfig( 'Database' , 'pre' ) );
		header('Content-type: text/html; charset=' . CHARSET );
		
		define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
		
		//设置本地时差
		function_exists('date_default_timezone_set') && date_default_timezone_set( 'Asia/Shanghai' );
		
		//压缩输出
		if(Common::getConfig( 'System' , 'gzip' ) && function_exists( 'ob_gzhandler' ) ) 
		{
			ob_start( 'ob_gzhandler' );
		} 
		else 
		{
			ob_start();
		}
		
		$ip = Ip::getCurrentIP();
		define( "IP" , $ip );
	}
	
	public static function session()
	{
		$sessionType = Common::getConfig( 'System' , 'session_type' );
		$sessionModel = Session::createSession( $sessionType );
		/*if ( Session::get( 'userId' ) && Session::get( 'userName' ) )
		{
			$userModel = User::getInstance( $_SESSION['userId'] , $_SESSION['userName'] );
		}*/
	}
	
	public static function param()
	{
		/*if ( !get_magic_quotes_gpc() )
		{
			$_POST = Core_Common::deep_addslashes( $_POST );
			$_GET = Core_Common::deep_addslashes( $_GET );
			$_COOKIES = Core_Common::deep_addslashes( $_COOKIES );
			$_REQUEST = Core_Common::deep_addslashes( $_REQUEST );
		}*/
		//define( 'PARAM_C' , $_GET['c'] ? $_GET['c'] : ( $_POST['c'] ? $_POST['c'] : 'index' ) );
		//define( 'PARAM_A' , $_GET['a'] ? $_GET['a'] : ( $_POST['a'] ? $_POST['a'] : 'run' ) );
	}
}

/**
 * 自动加载类文件
 *
 * @param string $className
 */
function __autoload( $className ) 
{
	//切分类的目录
	if( strpos( $className , "_" ) !== false )
	{
		$className = str_replace( "_" , "/" , $className );
	}
	
	//在模块文件夹搜索
	if( file_exists( MODEL . "/{$className}.php" ) )
	{
		include_once( MODEL . "/{$className}.php" );
		return ;
	}
	
    //在控制器文件夹搜索
	if( file_exists( CONTROLLER . "/{$className}.php" ) )
	{
		include_once( CONTROLLER . "/{$className}.php" );
		return ;
	}
}

Main::run();

?>